package com.desire.controller;

import com.desire.bean.UserBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LittleDesire
 * @description
 * @date 2022-04-22 周五 10:45
 * @since 17
 */
@Controller
public class IndexController {
    @RequestMapping
    public String index(Model m,Integer id) throws RuntimeException{
        m.addAttribute("msg", "index首页");
        m.addAttribute("id", id);
        if (id == 123) {
            throw new RuntimeException("id出问题了");
        }
        return "index";
    }

    @RequestMapping("json")
    @ResponseBody
    public UserBean json(){
        UserBean userBean = new UserBean();
        userBean.setId(1);
        userBean.setName("这哪行");
        return userBean;
    }

    @RequestMapping("/getAllUser")
    @ResponseBody
    public List<UserBean> getAllUser(){
        UserBean user = new UserBean();
        user.setId(666);
        user.setName("波波");

        UserBean user2 = new UserBean();
        user2.setId(123);
        user2.setName("波波1");
        List<UserBean> list = new ArrayList<>();
        list.add(user);
        list.add(user2);
        return list;
    }

    @PostMapping("/get")
    public void add(@RequestBody UserBean userBean) {
        System.out.println(userBean);
    }

    @RequestMapping("/person")
    public String add() {
        return "person";
    }
}
